package com.qf.service.impl;

import com.qf.dao.OrderDao;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.domain.*;
import com.qf.service.*;
import com.qf.utils.DateSourceUtil;

import java.sql.SQLException;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao=new OrderDaoImpl();
    @Override
    public void submitOrder(Order order, List<Cart> cartList) {
        try {
            //事务处理
            //手动开启事务处理
            DateSourceUtil.begin();
            //1向订单表中添加订单
            orderDao.add(order);
            //2向订单详情中添加数据******
            for (Cart cart : cartList) {
                OrderDetail orderDetail=new OrderDetail(0,order.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
                orderDao.addDetail(orderDetail);
            }
            //3清空购物车
            CartService cartService=new CartServiceImpl();
            cartService.clear(order.getUid());
            //事务提交
            DateSourceUtil.commit();
        } catch (SQLException e) {
            try {
                //手动处理事务 事务回滚
                DateSourceUtil.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            throw new RuntimeException(e);
        }finally {
            try {
                //手动处理事务 释放资源
                DateSourceUtil.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }

    @Override
    public List<Order> find(Integer uid) {
        List<Order> orderList=orderDao.select(uid);
        //遍历订单列表
        if (orderList != null&&orderList.size()>0) {
            //获取地址
            AddressService addressService=new AddressServiceImpl();
            List<Address> addressList = addressService.find(uid);
            if (addressList != null) {
                for (Order order : orderList) {
                    for (Address address : addressList) {
                        //订单表的aid字段与地址表的id匹配获取
                        if(order.getAid()==address.getId()){
                            order.setAddress(address);
                        }
                    }
                }
            }
        }

        return orderList;
    }

    @Override
    public Order findByOid(String oid) {
        Order order=orderDao.selectByOid(oid);
        if (order != null) {
            //手动封装地址信息  order.address.name
            AddressService addressService=new AddressServiceImpl();
            List<Address> addressList = addressService.find(order.getUid());
            if (addressList != null) {
                for (Address address : addressList) {
                    if (address.getId()==order.getAid()) {
                        order.setAddress(address);
                    }
                }
            }
        }
        return order;
    }

    @Override
    public List<OrderDetail> findDetail(String oid) {
        List<OrderDetail> orderDetails=orderDao.selectDetail(oid);
        if (orderDetails != null&&orderDetails.size()>=0) {
            //手动封装商品表  items.goods.name
            GoodsService goodsService=new GoodsServiceImpl();
            for (OrderDetail orderDetail : orderDetails) {
                Goods goods=goodsService.findById(orderDetail.getPid());
                orderDetail.setGoods(goods);
            }
        }
        return orderDetails;
    }

    @Override
    public List<Order> fAll(String where, List<Object> params) {
        List<Order> orderList = orderDao.sAll(where,params);
        if(orderList!=null&&!orderList.isEmpty()){
            for (Order order : orderList) {
                UserService userService=new UserServiceImpl();
                User user = userService.findById(order.getUid());
                order.setUser(user);
            }
        }
        return orderList;
    }
}
